package me.goldze.mvvmhabit.binding.viewadapter.image;

import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;

import androidx.databinding.BindingAdapter;

import java8.util.stream.Stream;
import me.goldze.mvvmhabit.R;
import me.goldze.mvvmhabit.utils.KLog;

/**
 * Created by goldze on 2017/6/18.
 */
public final class ViewAdapter {
    @BindingAdapter(value = {"url", "placeholderRes", "defaultUrl","gifRes","drawable"}, requireAll = false)
    public static void setImageUri(ImageView imageView, String url, int placeholderRes, int defaultUrl, int gifRes, Drawable bitmaps) {
        if (bitmaps!=null){
            RequestOptions options = new RequestOptions()
                    .fallback(defaultUrl) //url为空的时候,显示的图片
                    .bitmapTransform(new RoundedCorners(2))
                    .error(defaultUrl);//图片加载失败后，显示的图片;
            //使用Glide框架加载图片
            Glide.with(imageView.getContext())
                    .load(bitmaps)
                    .apply(options)
                    .into(imageView);
        }else
        if (gifRes!=0){
            RequestOptions options = new RequestOptions()
                    .fitCenter()
                    .skipMemoryCache(true)
                    .diskCacheStrategy(DiskCacheStrategy.DATA);
            //使用Glide框架加载图片
            Glide.with(imageView.getContext())
                    .asGif()
                    .load(gifRes)
                    .apply(options)
                    .into(imageView);
        }else {
            RequestOptions options = new RequestOptions()
                    .placeholder(placeholderRes)//图片加载出来前，显示的图片
                    .fallback(defaultUrl) //url为空的时候,显示的图片
                    .error(defaultUrl);//图片加载失败后，显示的图片;
            if (!TextUtils.isEmpty(url)) {
                options = options.
                        bitmapTransform(new RoundedCorners(10));
            }
            //使用Glide框架加载图片
            Glide.with(imageView.getContext())
                    .load(url)
                    .apply(options)
                    .into(imageView);
        }
    }

}

